#include <iostream>
#include <set>
#include <cmath>

using namespace std;

/**
 * 宠物收养场
 * 
 * https://www.luogu.com.cn/problem/P2286
 */
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    set<int> pets;
    set<int> customers;

    int n;
    cin >> n;

    long long sum = 0;
    for (int i = 0; i < n; i++) {
        int op, a;
        cin >> op >> a;

        if (op == 0) { // 新宠物
            if (customers.empty()) {
                pets.insert(a);
            }
            else {
                auto it = customers.lower_bound(a);

                if (it == customers.begin()) {
                    sum += abs(*it - a);
                    customers.erase(it);
                }
                else if (it == customers.end()) {
                    auto pre = prev(it);
                    sum += abs(*pre - a);
                    customers.erase(pre);
                }
                else {
                    auto pre = prev(it);
                    if (abs(*pre - a) <= abs(*it - a)) {
                        sum += abs(*pre - a);
                        customers.erase(pre);
                    }
                    else {
                        sum += abs(*it - a);
                        customers.erase(it);
                    }
                }
            }
        }
        else { // 新领养者
            if (pets.empty()) {
                customers.insert(a);
            }
            else {
                auto it = pets.lower_bound(a);

                if (it == pets.begin()) {
                    sum += abs(*it - a);
                    pets.erase(it);
                }
                else if (it == pets.end()) {
                    auto pre = prev(it);
                    sum += abs(*pre - a);
                    pets.erase(pre);
                }
                else {
                    auto pre = prev(it);
                    if (abs(*pre - a) <= abs(*it - a)) {
                        sum += abs(*pre - a);
                        pets.erase(pre);
                    }
                    else {
                        sum += abs(*it - a);
                        pets.erase(it);
                    }
                }
            }
        }
    }

    cout << sum % 1000000;

    return 0;
}
